"""
认证相关视图
"""

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
from rest_framework import status
from drf_spectacular.utils import extend_schema, OpenApiResponse, OpenApiRequest
from drf_spectacular.types import OpenApiTypes
from ..permissions import IsAdminUser


class CurrentUserView(APIView):
    """获取当前用户信息"""
    permission_classes = [IsAuthenticated, IsAdminUser]

    @extend_schema(
        summary='获取当前用户信息',
        description='获取当前登录用户的详细信息',
        tags=['认证'],
        responses={
            200: OpenApiResponse(
                description='用户信息',
                response={
                    'type': 'object',
                    'properties': {
                        'id': {'type': 'integer'},
                        'username': {'type': 'string'},
                        'email': {'type': 'string'},
                        'is_staff': {'type': 'boolean'},
                        'is_superuser': {'type': 'boolean'},
                    }
                }
            )
        }
    )
    def get(self, request):
        user = request.user
        return Response({
            'id': user.id,
            'username': user.username,
            'email': user.email,
            'is_staff': user.is_staff,
            'is_superuser': user.is_superuser,
        })


class ChangePasswordView(APIView):
    """修改密码"""
    permission_classes = [IsAuthenticated, IsAdminUser]

    @extend_schema(
        summary='修改密码',
        description='修改当前登录用户的密码',
        tags=['认证'],
        request=OpenApiRequest(
            request={
                'application/json': {
                    'type': 'object',
                    'properties': {
                        'old_password': {'type': 'string', 'description': '当前密码'},
                        'new_password': {'type': 'string', 'description': '新密码'},
                    },
                    'required': ['old_password', 'new_password']
                }
            }
        ),
        responses={
            200: OpenApiResponse(
                description='修改成功',
                response={
                    'type': 'object',
                    'properties': {
                        'message': {'type': 'string'}
                    }
                }
            ),
            400: OpenApiResponse(description='请求参数错误'),
        }
    )
    def post(self, request):
        user = request.user
        old_password = request.data.get('old_password')
        new_password = request.data.get('new_password')

        # 验证参数
        if not old_password or not new_password:
            return Response(
                {'error': '请提供当前密码和新密码'},
                status=status.HTTP_400_BAD_REQUEST
            )

        # 验证当前密码
        if not user.check_password(old_password):
            return Response(
                {'error': '当前密码不正确'},
                status=status.HTTP_400_BAD_REQUEST
            )

        # 验证新密码长度
        if len(new_password) < 6:
            return Response(
                {'error': '新密码长度至少为6位'},
                status=status.HTTP_400_BAD_REQUEST
            )

        # 设置新密码
        user.set_password(new_password)
        user.save()

        return Response({
            'message': '密码修改成功'
        })

